package com.cpi.cornpp.poplanned.reals.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.poplanned.reals.exception.PoPlannedRealsException;
import com.cpi.cornpp.poplanned.reals.form.PoPlannedRealsForm;
import com.cpi.cornpp.poplanned.reals.services.PoPlannedRealsService;
import com.cpi.cornpp.poplanned.reals.view.PoPlannedRealsView;

/**
 * PoPlanned Reals Add Action
 * @author Edgar Joao
 *
 */
public class PoPlannedRealsAddAction extends Action{
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Fail forward */
	private final static String FAIL_FORWARD = "fail";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/**
	 * Add a New PoPlanned Reals
	 * @author Edgar Joao
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		PoPlannedRealsForm poPlannedRealsForm = null;
		PoPlannedRealsService poPlannedRealsService = null;
		PoPlannedRealsView poPlannedRealsView = null;
		String messageError = null;
		String forward = null;
		
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			poPlannedRealsService = new PoPlannedRealsService();
			poPlannedRealsView = new PoPlannedRealsView();
			if(form != null){								
				poPlannedRealsForm = (PoPlannedRealsForm) form;
				poPlannedRealsView.setPoNumber(poPlannedRealsForm.getNumber());
				poPlannedRealsView.setPurchaseOrderId(poPlannedRealsForm.getPurchaseOrderId());
				poPlannedRealsService.addPoPlannedReals(poPlannedRealsView);
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY),SUCCESS_ADD_LABEL);
				httpSession.setAttribute(MESSAGE, messageError);
			}
			forward = DEFAULT_FORWARD;
		}catch (PoPlannedRealsException poPlannedRealsException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), poPlannedRealsException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, poPlannedRealsException);
				form.reset(mapping, request);
				throw poPlannedRealsException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAIL_FORWARD;
			}
		}catch (Exception exception){
			PoPlannedRealsException poPlannedRealsException = null;
			exception.printStackTrace(System.out);
			poPlannedRealsException= new PoPlannedRealsException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, poPlannedRealsException);
			throw poPlannedRealsException;
		}
		
		return mapping.findForward(forward);			
	}

}
